# -*- coding: utf-8 -*-
from socket import *


# params: connectIp:服务器IP, connectPort:服务器端口


class TcpClint():
    def __init__(self, connectIp, connectPort=8001):
        self.tcp_socket = None
        self.connectIp = connectIp
        self.connectPort = connectPort

    # 创建tcp连接服务器
    def connect(self):
        try:
            tcp_socket = socket(AF_INET, SOCK_STREAM)
            ip_port = (self.connectIp, self.connectPort)
            tcp_socket.connect(ip_port)
            self.tcp_socket = tcp_socket
        except Exception as e:
            print(e)

    # 发送消息
    def sendMsg(self):
        try:
            send_str = '1'
            self.tcp_socket.send(send_str.encode('utf-8'))
            self.tcp_socket.close()
        except Exception as e:
            print(e)

    # 创建tcp服务器并发送消息
    def send(self):
        try:
            self.connect()
            self.sendMsg()
        except Exception as e:
            print(e)

#
# if __name__ == '__main__':
#     tcpClint = TcpClint('192.168.61.115', 8001)
#     tcpClint.send()
